/*这里放置一些可以重用的接口调用*/
/*主要用于数据获取*/

var constract = require('/constract.js');
/**
 * 提高人气
 * 
 * @param {*} token 获取的token
 * @param {*} id 名片id
 */
function raisePopilarity(token,id){
  wx.request({
    url: constract.popilarity, //人气增加
    header: { token: token },
    data: { cardId: id },
    success: function (res) {}
  });
}


//图片识别上传
/**
 * 
 * @param {Array} _type  选择图片or拍照上传
 * @param {Object} that  对this的保留
 * @param {string} url   接口
 * @param {string} pages 哪个页面调用(card photograph) 没用上
 */
function ocrImg(_type,that,url,pages,uploadType) {
  wx.chooseImage({
    count: 1,
    sizeType: ['compressed'],
    sourceType: _type,

    success: function (res) {
      var tempFilePaths = res.tempFilePaths[0];
      
      wx.setNavigationBarTitle({
        title: '识别中...',
        success: function () {
          that.setData({
            show: false
          })
          if (wx.showLoading) {
            wx.showLoading({
              title: '识别中...',
              mask: true,
            });
          }
        }
      });

      wx.uploadFile({
        url: url,
        name: 'card',
        filePath: tempFilePaths,
        success: function (res) {
          console.log(res);
        console.log(res.data.replace(/[+?%#&=/\s*]/g,''));
          var codeData = JSON.parse(res.data).code;
          console.log('--------------');

          if (wx.hideLoading) {
            wx.hideLoading();
          }
          if (codeData == 17000) {
            wx.setNavigationBarTitle({
              title: '名名mm',
              success: function () {
                wx.showModal({
                  title: '提示',
                  content: '请上传正确清晰图片!',
                  showCancel: false
                }); 
              }
            });
            that.setData({
              show: true
            })
            return false;
          }

          that.setData({ // 这里在photograph页面使用了,card页面没有,需要做个判断
            show: true
          })

          wx.setNavigationBarTitle({
            title: '名名mm',
            success: function () {
              wx.navigateTo({
                url: '../input-card/input-card?msg=' + res.data.replace(/[+?%#&=\s*]/g,'') + '&uploadType=' + uploadType
              });
            }
          });
        }
      })
    }
  })
}

/**/

/**/
function gotUserInfo(app,e,that){
  console.log(e);
  app.Verification(function(token){

    if(e.detail.userInfo) {
      var avatar = e.detail.userInfo.avatarUrl;
      var name = e.detail.userInfo.nickName;

      app.globalData.nickName = name;
      app.globalData.avatarUrl = avatar;

      var userInfo = that.data.userInfo;
      userInfo.name = app.globalData.nickName;
      userInfo.avatar = app.globalData.avatarUrl;

      // that.setData({
      //   userInfo: userInfo,
      // })

      wx.navigateTo({
        url: '/pages/add-card/add-card?avatar='+avatar+'&name='+name
      })
      that.setData({
        promptShow: false
      })
    }
  })
}


module.exports = {
  raisePopilarity: raisePopilarity,
  ocrImg: ocrImg,
  gotUserInfo: gotUserInfo
}
